package com.fengye.jpa.converter.model.many2many;

import com.fengye.jpa.converter.model.BaseEntity;
import lombok.*;

import javax.persistence.*;
import java.util.Objects;
import java.util.Set;

/**
 * @Author : sky
 * @Email : <mailto:fengyexjtu@126.com>
 * @Date: 2021/04/12 20:52
 * @Description:
 */

@Entity
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Author extends BaseEntity {

    private String name;
    private int age;

    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(name = "author_book",
            joinColumns = {@JoinColumn(name = "author_id", referencedColumnName = "id")},
            inverseJoinColumns = {@JoinColumn(name = "book_id", referencedColumnName = "id")}
    )
    private Set<Book> books;

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        if (!super.equals(o)) return false;
        Author book = (Author) o;
        return this.getId() == book.getId();
    }

    @Override
    public int hashCode() {
        return Objects.hash(super.hashCode(), this.getId());
    }

    @Override
    public String toString() {
        return "Author{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
